const { DataTypes } = require('sequelize')

module.exports = (sequelize) => {
  const Order = sequelize.define('Order', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    order_no: {
      type: DataTypes.STRING(50),
      allowNull: false,
      unique: true,
      comment: '订单号'
    },
    customer_id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      comment: '客户ID'
    },
    order_date: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW,
      comment: '订单日期'
    },
    delivery_date: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '交货日期'
    },
    status: {
      type: DataTypes.ENUM('draft', 'pending', 'confirmed', 'processing', 'shipped', 'delivered', 'cancelled', 'returned'),
      defaultValue: 'draft',
      comment: '订单状态'
    },
    payment_status: {
      type: DataTypes.ENUM('unpaid', 'partial', 'paid', 'refunded'),
      defaultValue: 'unpaid',
      comment: '付款状态'
    },
    payment_method: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '付款方式'
    },
    currency: {
      type: DataTypes.STRING(10),
      defaultValue: 'CNY',
      comment: '币种'
    },
    subtotal: {
      type: DataTypes.DECIMAL(12, 2),
      allowNull: false,
      defaultValue: 0,
      comment: '小计金额'
    },
    tax_amount: {
      type: DataTypes.DECIMAL(12, 2),
      defaultValue: 0,
      comment: '税费'
    },
    discount_amount: {
      type: DataTypes.DECIMAL(12, 2),
      defaultValue: 0,
      comment: '折扣金额'
    },
    shipping_fee: {
      type: DataTypes.DECIMAL(12, 2),
      defaultValue: 0,
      comment: '运费'
    },
    total_amount: {
      type: DataTypes.DECIMAL(12, 2),
      allowNull: false,
      defaultValue: 0,
      comment: '总金额'
    },
    paid_amount: {
      type: DataTypes.DECIMAL(12, 2),
      defaultValue: 0,
      comment: '已付金额'
    },
    shipping_address: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '收货地址JSON'
    },
    billing_address: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '账单地址JSON'
    },
    notes: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '订单备注'
    },
    internal_notes: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '内部备注'
    },
    created_by: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '创建者ID'
    },
    created_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    },
    updated_at: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: DataTypes.NOW
    }
  }, {
    tableName: 'orders',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    comment: '订单表',
    indexes: [
      {
        unique: true,
        fields: ['order_no']
      },
      {
        fields: ['customer_id']
      },
      {
        fields: ['status']
      },
      {
        fields: ['payment_status']
      },
      {
        fields: ['order_date']
      }
    ]
  })

  return Order
}